package cloud.shyidong.admin.tenant.service;

import cloud.shyidong.core.request.LongIdRequest;
import cloud.shyidong.core.service.AbstractService;
import cloud.shyidong.core.util.ObjectUtils;
import cloud.shyidong.tenant.dao.TenantProjectDao;
import cloud.shyidong.tenant.entity.TenantProjectEntity;
import cloud.shyidong.tenant.request.TenantProjectInsertRequest;
import cloud.shyidong.tenant.request.TenantProjectPageRequest;
import cloud.shyidong.tenant.request.TenantProjectSaveRequest;
import cloud.shyidong.tenant.request.TenantProjectUpdateRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 租户项目业务处理
 * </p>
 *
 * @author syd
 * @since 2023-09-18 15:34:26
 */
@Service
public class TenantProjectService extends AbstractService<TenantProjectDao, TenantProjectEntity, LongIdRequest, TenantProjectInsertRequest, TenantProjectUpdateRequest, TenantProjectPageRequest> {
    
    public TenantProjectEntity getByTenantId(Long id) {
        QueryWrapper<TenantProjectEntity> tenantProjectEntityQueryWrapper = new QueryWrapper<>();
        tenantProjectEntityQueryWrapper.eq("tenant_id", id);
        return getDao().selectOne(tenantProjectEntityQueryWrapper);
    }
    
    public TenantProjectEntity save(TenantProjectSaveRequest tenantProjectSaveRequest) {
        TenantProjectEntity tenantProjectEntity = getByTenantId(tenantProjectSaveRequest.getTenantId());
        if (ObjectUtils.isNull(tenantProjectEntity)) {
            TenantProjectInsertRequest tenantProjectInsertRequest = mapper.map(tenantProjectSaveRequest, TenantProjectInsertRequest.class);
            return this.insert(tenantProjectInsertRequest);
        }
        TenantProjectUpdateRequest tenantProjectUpdateRequest = mapper.map(tenantProjectSaveRequest, TenantProjectUpdateRequest.class);
        return this.update(tenantProjectUpdateRequest);
    }
    
}
